<template>
  <div class="m-25 mt-65">
    <div class="normal-box-mod">

      <!-- title -->
      <div class="normal-box-hd clearfix">
        <div class="normal-box-title">
          <div class="decorate-square"></div>
          <h2>商户列表</h2>
        </div>
      </div>

      <!-- 搜索 -->
      <div class="normal-box-bd">
        <div class="clearfix mb-20">
          <Button custom-icon="iconfont icon-fangkuai2" type="primary" size="small" style="float:right"
            @click="show_create">添加商户</Button>
          <Input class="mr-10" v-model="queryData.businessName" placeholder="商户名称" style="width: 200px" />
          <Col span="5" class="mr-10">
          <sele @ProvinceData="childByValue"></sele>
          </Col>
          <Button type="primary" custom-icon="iconfont icon-chaxun" size="small" @click="getList">查找</Button>
        </div>
      </div>

      <div>
        <i-table stripe highlight-row border :content="self" :columns="Tcolumns" :data="adminList"></i-table>
      </div>
      <div class="page">
        <Page :total="queryData.totalRecord" :page-size="queryData.limit" :current="queryData.page" show-total
          show-elevator @on-change="changepage"></Page>
      </div>

      <!--新建/修改商户-->
      <Modal v-model="newAdmin.show_modal" width="900" :title="newAdmin.modal_title" :loading="newAdmin.loading"
        @on-ok="sendPost">
        <Form ref="check" :model="newAdmin.data" :label-width="100" inline>
          <FormItem label="商户类型" prop="businessType">
            <Select v-model="newAdmin.data.businessType" style="width:300px">
              <Option v-for="item in businessTypeList" :value="item.value" :key="item.value">{{ item.label }}</Option>
            </Select>
          </FormItem>
          <FormItem label="商户名称" prop="businessName">
            <Input placeholder="请输入" v-model="newAdmin.data.businessName" style="width:300px;" />
          </FormItem>
          <FormItem label="手机号码" prop="businessMobile" :rules="newAdmin.rules.telPhone">
            <Input placeholder="请输入" v-model="newAdmin.data.businessMobile" style="width:300px;" />
          </FormItem>
          <FormItem label="收费明细" prop="feeNum">
            <Input placeholder="请输入" type="number" v-model="newAdmin.data.feeNum" style="width:300px;" />
          </FormItem>
          <FormItem label="开户行名称" prop="bankName">
            <Input placeholder="请输入" v-model="newAdmin.data.bankName" style="width:300px;" />
          </FormItem>

          <FormItem label="持卡人姓名" prop="cardName">
            <Input placeholder="请输入" v-model="newAdmin.data.cardName" style="width:300px;" />
          </FormItem>
          <FormItem label="银行卡账号" prop="cardNum">
            <Input placeholder="请输入" v-model="newAdmin.data.cardNum" style="width:300px;" />
          </FormItem>
          <FormItem label="支付宝名称" prop="alipayName">
            <Input placeholder="请输入" v-model="newAdmin.data.alipayName" style="width:300px;" />
          </FormItem>
          <FormItem label="支付宝账号" prop="alipayNum">
            <Input placeholder="请输入" v-model="newAdmin.data.alipayNum" style="width:300px;" />
          </FormItem>
          <FormItem label="微信支付名称" prop="wxpayName">
            <Input placeholder="请输入" v-model="newAdmin.data.wxpayName" style="width:300px;" />
          </FormItem>
          <FormItem label="微信支付账号" prop="wxpayNum">
            <Input placeholder="请输入" v-model="newAdmin.data.wxpayNum" style="width:300px;" />
          </FormItem>
          <FormItem label="地址">
            <sele @ProvinceData="childByValue1" style="width:300px"></sele>
          </FormItem>
          <!-- <FormItem label="地址" v-if="!newAdmin.is_create">
            <cityNewBusiness @ProvinceData="childByValue1" :toChildrenProvincialData='toChildrenProvincialData'
              style="width:320px"></cityNewBusiness>
          </FormItem> -->
          <FormItem label="详细地址" prop="businessAddress">
            <Input placeholder="请输入" v-model="newAdmin.data.businessAddress" style="width:300px;" />
          </FormItem>
          <FormItem label="收费标准" prop="feeFlag">
            <RadioGroup v-model="newAdmin.data.feeFlag">
              <Radio label="0">免费</Radio>
              <Radio label="1">固定收费</Radio>
              <Radio label="2">营收百分比</Radio>
            </RadioGroup>
          </FormItem>
          <FormItem label="结算方式" prop="settleType">
            <RadioGroup v-model="newAdmin.data.settleType">
              <Radio label="0">银行卡</Radio>
              <Radio label="1">支付宝</Radio>
              <Radio label="2">微信支付</Radio>
              <Radio label="3">现金结算</Radio>
            </RadioGroup>
          </FormItem>

        </Form>
      </Modal>

      <!-- 商户管理员 -->
      <Modal width="1000" v-model="logShow" footer-hide title="商户管理员">
        <div>
          <div style="display:flex;margin-bottom:10px">
            <!-- <Input class="mr-10"
                   v-model="queryLog.businessName"
                   placeholder="商户名称"
                   style="width: 200px" />

            <sele @ProvinceData="childByValue2"
                  class="mr-10"
                  style="width:200px"></sele>
            <Button type="primary"
                    custom-icon="iconfont icon-chaxun"
                    size="small"
                    @click="getLog">查找</Button> -->

          </div>
          <Button type="primary" custom-icon="iconfont icon-fangkuai2" size="small" style="margin-bottom:10px"
            @click="showAdmin">添加商户管理员</Button>
          <Table stripe highlight-row border :content="self" :columns="logCol" :data="logList"></Table>
        </div>
        <div class="page">
          <Page :total="queryLog.totalRecord" :page-size="queryLog.limit" :current="queryLog.page" show-total
            show-elevator @on-change="logCpage"></Page>
        </div>
      </Modal>
      <!--新建/修改商户管理员-->
      <Modal v-model="admin.show_modal" width="600" :title="admin.modal_title" :loading="admin.loading" @on-ok="addAdmin">
        <Form ref="admin" :model="admin.data" :label-width="100">
          <FormItem label="管理员" prop="adminId">
            <Select v-model="admin.data.adminId" @on-change=changeAdmin label-in-value
              :default-label="admin.data.adminName" style="width:300px">
              <Option v-for="(ad, index) in selectAdmin" :key="ad.id" :value="ad.id">{{ ad.name }}</Option>
            </Select>
          </FormItem>
          <FormItem label="管理员身份" prop="adminIdentity">
            <RadioGroup v-model="admin.data.adminIdentity">
              <Radio label="0">老板 </Radio>
              <Radio label="1">员工</Radio>
            </RadioGroup>
          </FormItem>

        </Form>
      </Modal>
      <!-- 商户商品 -->
      <Modal width="1000" v-model="goodsShow" footer-hide title="商户商品">
        <div>
          <!-- <div style="display:flex;margin-bottom:10px">
            <Input class="mr-10"
                   v-model="queryLog.businessName"
                   placeholder="商户名称"
                   style="width: 200px" />
showPro
            <sele @ProvinceData="childByValue3"
                  class="mr-10"
                  style="width:200px"></sele>
            <Button type="primary"
                    custom-icon="iconfont icon-chaxun"
                    size="small"
                    @click="getGoods">查找</Button>

          </div> -->
          <Button type="primary" custom-icon="iconfont icon-fangkuai2" size="small" style="margin-bottom:10px"
            @click="showPro">添加商户商品</Button>
          <div>
            <Table stripe highlight-row border :content="self" :columns="goodsCol" :data="goodsList">

            </Table>
          </div>
        </div>
        <div class="page">
          <Page :total="queryGoods.totalRecord" :page-size="queryGoods.limit" :current="queryGoods.page" show-total
            show-elevator @on-change="goodsCpage"></Page>
        </div>
      </Modal>
      <!--新建/修改商户商品-->
      <Modal v-model="goods.show_modal" width="900" :title="goods.modal_title" :loading="goods.loading" @on-ok="addGoods">
        <Form ref="goods" :model="goods.data" :label-width="100" inline>
          <FormItem label="商品" prop="goods">
            <Select v-model="goods.data.goodsId" label-in-value @on-change=changeGoods style="width:300px">
              <Option v-for="(ad, index) in selectGoods" :key="ad.id" :value="ad.id">{{ ad.productName }}</Option>
            </Select>
          </FormItem>
          <FormItem label="进货价格" prop="purchasePrice">
            <Input placeholder="请输入" v-model="goods.data.purchasePrice" style="width:300px;" disabled type="number" />
          </FormItem>
          <FormItem label="销售价" prop="retailPrice">
            <Input placeholder="请输入" v-model="goods.data.retailPrice" style="width:300px;" type="number" />
          </FormItem>
          <FormItem label="会员价" prop="memberPrice">
            <Input placeholder="请输入" v-model="goods.data.memberPrice" style="width:300px;" type="number" />
          </FormItem>
          <FormItem label="库存数" prop="stockNum">
            <Input placeholder="请输入" v-model="goods.data.stockNum" style="width:300px;" type="number" />
          </FormItem>

        </Form>
      </Modal>
      <Modal v-model="modal.mallAdvertising.bigPicVisible" width="1000px">
        <img :src="modal.mallAdvertising.bigPicUrl" style="width: 50%; height: 50%; display: block; margin: 0 auto" />
        <div slot="footer" />
      </Modal>
      <!-- 入库 -->
      <Modal v-model="modal.put.isShow" width="500" title="入库" :loading="modal.put.loading" @on-ok="put">
        <Form ref="put" :model="modal.put.formData" :label-width="100" inline>
          <FormItem label="入库数量" prop="stockNum">
            <Input placeholder="请输入" v-model="modal.put.formData.stockNum" style="width:300px;" type="number" />
          </FormItem>

        </Form>
      </Modal>
    </div>
  </div>
</template>

<script>
import $api from '@/api/base_sys/gcDispenser.js'
import sele from '@/view/test/city.vue'
import BusinessApi from '@/api/business/business.js'
import AdminApi from '@/api/business/admin.js'
import GoodsApi from '@/api/business/goods.js'
// import seleNew from '@/view/test/cityNew.vue'
// import cityNewBusiness from '@/view/test/cityNewBusiness.vue'

export default {
  components: {
    sele,
    // cityNewBusiness
  },
  mounted() {
    let area = window.defArea
    this.queryData.provid = area[0]
    this.queryData.cityid = area[1]
    this.queryData.distid = area[2]
    this.getList()
  },
  data() {
    return {
      toChildrenProvincialData: [],
      businessTypeList: [
        { value: 'CGPS', label: '采配中心' },
        { value: 'ZIYN', label: '村口驿站' },
        { value: 'HEZO', label: '合作商盟' }
      ],
      adminList: [],
      url: '/api/mallAdmin/',
      keyWords: '', // 搜索关键字
      index: '', // 当前编号
      modal: {
        mallAdvertising: {
          isShow: false,
          bigPicVisible: false,
          loading: true,
          bigPicUrl: ''
        },
        put: {
          isShow: false,
          loading: true,
          formData: {}

        }
      },
      // 商户管理员下拉
      selectAdmin: [],
      selectGoods: [],
      // 商品下拉
      selectPro: [],
      // 添加商户
      newAdmin: {
        show_modal: false,
        is_create: true,
        loading: true,
        modal_title: '添加商户',
        rules: {
          // check
          businessType: [
            { required: true, message: '请填写商户类型', trigger: 'blur' }
          ],
          businessName: [
            { required: true, message: '请填写商户名称', trigger: 'blur' }
          ],
          feeFlag: [
            { required: true, message: '请选择收费标准', trigger: 'blur' }
          ],
          feeNum: [
            { required: true, message: '请填写收费明细', trigger: 'blur' }
          ],
          settleType: [
            { required: true, message: '请选择结算方式', trigger: 'blur' }
          ]
          // status:   [{required: true, message: "请选择账号状态", trigger: "blur"}],
        },
        data: {}
      },
      // 添加管理员
      admin: {
        show_modal: false,
        is_create: true,
        loading: true,
        modal_title: '添加管理员',
        data: {}
      },
      // 添加商户商品
      goods: {
        show_modal: false,
        is_create: true,
        loading: true,
        modal_title: '添加商户商品',
        data: {}
      },
      // 获取数据配置
      queryData: {
        limit: 10,
        page: 1,
        totalRecord: 0,
        businessName: ''
      },

      // 商户管理员参数
      queryLog: {
        limit: 10,
        page: 1,
        totalRecord: ''
      },
      logShow: false, // 商户管理员是否显示
      logList: [], // 商户管理员列表
      logCol: [
        { align: 'center', title: '序号', type: 'index', width: 80 },
        {
          align: 'center',
          title: '商户名称',
          key: 'businessName',
          minWidth: 100
        },
        { align: 'center', title: '管理员id', key: 'adminId', width: 100 },
        { align: 'center', title: '管理员姓名', key: 'adminName', width: 100 },
        {
          align: 'center',
          title: '管理员手机号码',
          key: 'adminMobile',
          width: 120
        },
        {
          align: 'center',
          title: '管理员身份',
          key: 'adminIdentity',
          width: 100,
          render: (create, params) => {
            let s = params.row.adminIdentity
            let text
            if (s == 0) {
              text = '老板'
            } else if (s == 1) {
              text = '员工'
            }
            return create(
              'span',
              {
                style: {}
              },
              text
            )
          }
        },
        {
          // 账号状态
          align: 'center',
          title: '账号状态',
          width: 100,
          render: (create, params) => {
            let s = params.row.state
            let text
            if (s == 0) {
              text = '正常'
            } else if (s == 1) {
              text = '停用'
            }
            return create(
              'span',
              {
                style: {}
              },
              text
            )
          }
        },
        { align: 'center', title: '添加时间', key: 'insertTime', width: 100 },
        {
          // 操作
          title: '操作',
          key: 'action',
          width: 150,
          align: 'center',
          fixed: 'right',
          render: (create, params) => {
            // 创建父节点
            return create('div', [
              create(
                'i-button',
                {
                  props: { type: 'success', size: 'small' },
                  style: { margin: '0 3px' },
                  on: {
                    click: () => {
                      this.admin.is_create = false
                      this.showAdmin(params.row)
                    }
                  }
                },
                '修改'
              ),
              create(
                'i-button',
                {
                  props: { type: 'warning', size: 'small' },
                  style: { margin: '0 3px' },
                  on: {
                    click: () => {
                      this.$Modal.confirm({
                        title: '删除',
                        content:
                          '<p>你确认删除管理员 <strong class="color-red">' +
                          params.row.adminName +
                          '</strong> 吗，此操作不可逆，请谨慎操作！</p>',
                        onOk: () => {
                          AdminApi.del(params.row.id).then(() => {
                            this.getLog()
                            this.$Message.success('删除成功')
                          })
                        }
                      })
                    }
                  }
                },
                '删除'
              )
            ])
          }
        }
      ],
      // 商户商品查询参数
      queryGoods: {
        limit: 5,
        page: 1,
        totalRecord: ''
      },
      goodsShow: false, // 商户商品是否显示
      goodsList: [], // 商户商品列表
      goodsCol: [
        { align: 'center', title: '序号', type: 'index', width: 50 },
        { align: 'center', title: '商户名称', key: 'businessName', width: 100 },
        { align: 'center', title: '商品名称', key: 'goodsName', minWidth: 100 },
        {
          align: 'center',
          title: '商品条形码',
          key: 'goodsCode',
          width: 100
          //   render: (h, params) => {
          //     return h('img', {
          //       attrs: {
          //         src: params.row.goodsCode
          //       },
          //       style: {
          //         width: '100%'
          //         //   height: "35px",
          //       },
          //       on: {
          //         click: () => {
          //           this.showBigPic(params.row.imgUrl) // 此处再用一个方法，建一个modal把图放大。
          //         }
          //       }
          //     })
          //   }
        },
        {
          align: 'center',
          title: '商品缩略图',
          key: 'thumbnail',
          width: 80,
          render: (h, params) => {
            console.log(params.row,'11111111111')
            return h('img', {
              attrs: {
                src: params.row.thumbnail
              },
              style: {
                width: '100%'
                //   height: "35px",
              },
              on: {
                click: () => {
                  this.showBigPic(params.row.thumbnail) // 此处再用一个方法，建一个modal把图放大。
                }
              }
            })
          }
        },
        {
          align: 'center',
          title: '进货价格',
          key: 'purchasePrice',
          width: 80
        },
        {
          align: 'center',
          title: '销售价格',
          key: 'retailPrice',
          width: 80
        },
        {
          align: 'center',
          title: '会员价格',
          key: 'memberPrice',
          width: 80
        },
        {
          align: 'center',
          title: '库存数目',
          key: 'stockNum',
          width: 80
        },
        {
          align: 'center',
          title: '已销售数目',
          key: 'saleNum',
          width: 80
        },
        {
          align: 'center',
          title: '上架时间',
          key: 'insertTime',
          width: 100
        },
        {
          // 操作
          title: '操作',
          key: 'action',
          width: 200,
          align: 'center',
          fixed: 'right',
          render: (create, params) => {
            // 创建父节点
            return create('div', [
              create(
                'i-button',
                {
                  props: { type: 'success', size: 'small' },
                  style: { margin: '0 3px' },
                  on: {
                    click: () => {
                      this.goods.is_create = false
                      this.showPro(params.row)
                    }
                  }
                },
                '修改'
              ),
              create(
                'i-button',
                {
                  props: { type: 'warning', size: 'small' },
                  style: { margin: '0 3px' },
                  on: {
                    click: () => {
                      this.$Modal.confirm({
                        title: '删除',
                        content:
                          '<p>你确认删除商品 <strong class="color-red">' +
                          params.row.goodsName +
                          '</strong> 吗，此操作不可逆，请谨慎操作！</p>',
                        onOk: () => {
                          GoodsApi.del(params.row.id).then(() => {
                            this.getGoods()
                            this.$Message.success('删除成功')
                          })
                        }
                      })
                    }
                  }
                },
                '删除'
              ),
              create(
                'i-button',
                {
                  props: { type: 'primary', size: 'small' },
                  style: { margin: '0 3px' },
                  on: {
                    click: () => {
                      this.showPut(params.row)
                    }
                  }
                },
                '入库'
              )
            ])
          }
        }
      ],

      // 表格配置
      self: this,
      Tcolumns: [
        { align: 'center', title: '序号', type: 'index', width: 80 },
        { align: 'center', title: '商户名称', key: 'businessName', width: 150 },
        {
          align: 'center',
          title: '商户电话',
          minWidth: 150,
          key: 'businessMobile'
        },
        {
          align: 'center',
          title: '商户类型',
          minWidth: 100,
          key: 'businessType',
          render: (create, params) => {
            let s = params.row.businessType
            let text
            if (s == 'CGPS') {
              text = '采配中心'
            } else if (s == 'ZIYN') {
              text = '村口驿站'
            } else if (s == 'HEZO') {
              text = '合作商盟'
            }
            return create(
              'span',
              {
                style: {}
              },
              text
            )
          }
        },
        {
          align: 'center',
          title: '地址',
          minWidth: 100,
          key: 'businessAddress'
        },
        { align: 'center', title: '销售总额', minWidth: 80, key: 'totalTrade' },
        {
          // 账号状态
          align: 'center',
          title: '收费标准',
          width: 100,
          render: (create, params) => {
            let s = params.row.feeFlag
            let text
            if (s == 0) {
              text = '免费'
            } else if (s == 1) {
              text = '固定收费'
            } else if (s == 2) {
              text = '营收百分比'
            }
            return create(
              'span',
              {
                style: {}
              },
              text
            )
          }
        },
        { align: 'center', title: '费用明细', key: 'feeNum', width: 160 },
        {
          // 结算方式
          align: 'center',
          title: '结算方式',
          width: 100,
          render: (create, params) => {
            let s = params.row.settleType
            let text
            if (s == 0) {
              text = '银行卡'
            } else if (s == 1) {
              text = '支付宝'
            } else if (s == 2) {
              text = '微信支付'
            } else if (s == 3) {
              text = '现金结算'
            }
            return create(
              'span',
              {
                style: {}
              },
              text
            )
          }
        },
        {
          // 商户状态
          align: 'center',
          title: '商户状态',
          width: 100,
          render: (create, params) => {
            let s = params.row.status
            let text
            if (s == 0) {
              text = '正常'
            } else if (s == 1) {
              text = '冻结'
            } else if (s == 2) {
              text = '服务到期'
            }
            return create(
              'span',
              {
                style: {}
              },
              text
            )
          }
        },
        {
          // 操作
          title: '操作',
          key: 'action',
          width: 300,
          align: 'center',
          fixed: 'right',
          render: (create, params) => {
            // 创建父节点
            return create('div', [
              // 商户管理员
              create(
                'i-button',
                {
                  props: { type: 'info', size: 'small' },
                  style: { margin: '0 3px' },
                  on: {
                    click: () => {
                      this.showLog(params.row)
                    }
                  }
                },
                '商户管理员'
              ),
              // 商户商品
              create(
                'i-button',
                {
                  props: { type: 'primary', size: 'small' },
                  style: { margin: '0 3px' },
                  on: {
                    click: () => {
                      this.showGoods(params.row)
                    }
                  }
                },
                '商户商品'
              ),
              create(
                'i-button',
                {
                  props: { type: 'success', size: 'small' },
                  style: { margin: '0 3px' },
                  on: {
                    click: () => {
                      this.newAdmin.is_create = false
                      this.show_create(params.row)
                    }
                  }
                },
                '修改'
              ),
              create(
                'i-button',
                {
                  props: { type: 'warning', size: 'small' },
                  style: { margin: '0 3px' },
                  on: {
                    click: () => {
                      this.$Modal.confirm({
                        title: '删除',
                        content:
                          '<p>你确认删除商户名称<strong class="color-red">' +
                          params.row.businessName +
                          '</strong> 吗，此操作不可逆，请谨慎操作！</p>',
                        onOk: () => {
                          BusinessApi.del(params.row.id).then(() => {
                            this.$Message.success('删除成功')
                            this.getList()
                          })
                        }
                      })
                    }
                  }
                },
                '删除'
              )
            ])
          }
        }
      ]
    }
  },
  async created() {
    // this.getList()
    this.getAdminList()
    this.getGoodsList()
  },
  methods: {
    // 获取初始化管理员
    async getAdminList() {
      this.selectAdmin = (await AdminApi.init(this.queryLog)).data
    },
    async getGoodsList() {
      this.selectGoods = (await GoodsApi.init(this.queryGoods)).data
    },
    showBigPic(item) {
      this.modal.mallAdvertising.bigPicVisible = true
      this.modal.mallAdvertising.bigPicUrl = item
    },
    // 六级联动数据获取
    childByValue(data) {
      let area = this.queryData
      area.provid = data.provid || ''
      area.cityid = data.cityid || ''
      area.distid = data.distid || ''
      area.strtid = data.strtid || ''
      area.commid = data.commid || ''
      area.comminid = data.comminid || ''
    },
    // 六级联动数据获取
    childByValue1(data) {

      let area = this.newAdmin.data
      area.provid = data.provid || ''
      area.cityid = data.cityid || ''
      area.distid = data.distid || ''
      area.strtid = data.strtid || ''
      area.commid = data.commid || ''
      area.comminid = data.comminid || ''
    },
    // 六级联动数据获取
    childByValue2(data) {
      let area = this.queryLog
      area.provid = data.provid || ''
      area.cityid = data.cityid || ''
      area.distid = data.distid || ''
      area.strtid = data.strtid || ''
      area.commid = data.commid || ''
      area.comminid = data.comminid || ''
    },
    // 六级联动数据获取
    childByValu3(data) {
      let area = this.queryGoods
      area.provid = data.provid || ''
      area.cityid = data.cityid || ''
      area.distid = data.distid || ''
      area.strtid = data.strtid || ''
      area.commid = data.commid || ''
      area.comminid = data.comminid || ''
    },
    showLog(row) {
      this.logShow = true
      this.queryLog.businessId = row.id
      this.queryLog.businessName = row.businessName
      this.admin.data.businessId = row.id
      this.admin.data.businessName = row.businessName
      this.getLog()
    },
    showGoods(row) {
      this.goodsShow = true
      this.queryGoods.businessId = row.id
      this.queryGoods.businessName = row.businessName
      this.goods.data.businessId = row.id
      this.goods.data.businessName = row.businessName
      this.getGoods()
    },
    showAdmin(row) {
      let qwq = this.admin
      if (!row.id) {
        qwq.is_create = true
      }
      if (qwq.is_create) {
        qwq.is_create = true
        qwq.modal_title = '添加商户管理员'
        qwq.show_modal = true // 模态框显示
        // this.getAdminList()
        this.$refs['admin'].resetFields()
      } else {
        qwq.is_create = false
        qwq.modal_title = '修改商户管理员'
        // this.getAdminList()
        this.$refs['admin'].resetFields()
        qwq.data = row
        qwq.show_modal = true
      }
    },
    showPro(row) {
      let qwq = this.goods
      if (!row.id) {
        qwq.is_create = true
      }
      if (qwq.is_create) {
        qwq.is_create = true
        qwq.modal_title = '添加商户商品'
        qwq.show_modal = true // 模态框显示
        this.$refs['goods'].resetFields()
      } else {
        qwq.is_create = false
        qwq.modal_title = '修改商户商品'
        this.$refs['goods'].resetFields()
        qwq.data = row
        qwq.show_modal = true // 模态框显示
      }
    },
    //
    showPut(data) {
      this.modal.put.formData.businessId = data.businessId
      this.modal.put.formData.businessName = data.businessName
      this.modal.put.formData.id = data.id
      this.modal.put.formData.goodsName = data.goodsName
      this.modal.put.isShow = true
    },
    // 获取商户管理员
    async getLog() {
      let data = (await AdminApi.getPageList(this.queryLog)).data
      this.queryLog.totalRecord = data.totalRecord
      this.logList = data.list
    },
    // 获取商户商品
    async getGoods() {
      let data = (await GoodsApi.getPageList(this.queryGoods)).data
      this.queryGoods.totalRecord = data.totalRecord
      this.goodsList = data.list
    },
    // 商品库存分页
    logCpage(index) {
      this.queryLog.page = index
      this.getLog()
    },
    goodsCpage(index) {
      this.queryGoods.page = index
      this.getGoods()
    },
    // 获取商户列表
    async getList() {
      let data = (await BusinessApi.getPageList(this.queryData)).data
      this.queryData.totalRecord = data.totalRecord
      this.adminList = data.list
    },

    // 分页切换
    changepage(index) {
      this.queryData.page = index
      this.getList()
    },

    // 新建用户模态框
    show_create(row) {
      if (!row.id) {
        this.newAdmin.is_create = true
      }
      if (this.newAdmin.is_create) {
        this.newAdmin.data = {}
        this.newAdmin.rules.businessMobile = [
          {
            required: true,
            min: 11,
            max: 11,
            message: '请填写正确的手机号',
            trigger: 'blur'
          },
          {
            type: 'number',
            trigger: 'blur',
            message: '手机号码格式错误',
            transform(value) {
              return Number(value)
            }
          }
        ]
        let qwq = this.newAdmin
        this.$refs['check'].resetFields() // 清空内容
        qwq.is_create = true
        qwq.modal_title = '添加商户'
        qwq.show_modal = true // 模态框显示
      } else {
        // let provincialData = [
        //   row.provid,
        //   row.cityid,
        //   row.distid,
        //   row.strtid,
        //   row.commid,
        //   row.comminid
        // ]
        // this.toChildrenProvincialData = provincialData
        // console.log(this.toChildrenProvincialData,'this.toChildrenProvincialData')
        let qwq = this.newAdmin
        // this.$refs['check'].resetFields() // 清空内容
        qwq.data = row
        qwq.is_create = false
        qwq.modal_title = '修改商户'
        qwq.show_modal = true // 模态框显示
      }
    },

    // 修改用户模态框
    async show_update(index) {
      this.index = index
      this.newAdmin.rules.mobilephone = []
    //   this.$refs['check'].resetFields()
      let qwq = this.newAdmin
      qwq.rules.id = []
      qwq.is_create = false
      qwq.modal_title = '修改管理员'
      this.queryData.userId = index
      let data = (await $api.getData(this.url + index)).data
      this.clearQuery()

      // 数据填充
      let userData = this.newAdmin.data
      userData.id = data.id
      userData.name = data.name
      userData.telPhone = data.telPhone
      userData.password = data.password
      //   userData.status = String(Number(data.status))
      userData.status = data.status
      qwq.show_modal = true // 模态框显示
    },

    // 查询清空
    clearQuery() {
      this.queryData.name = ''
      this.queryData.telPhone = ''
    },

    // 新增商户
    sendPost() {
      this.$refs['check'].validate(async (valid) => {
        if (!valid) {
          this.changeLoading()
          return
        }
        if (valid) {
          // this.newAdmin.data.status = Boolean(Number(this.newAdmin.data.status))
          if (this.newAdmin.is_create) {
            let r = await BusinessApi.add(this.newAdmin.data)
            let message = r.message
            if (r.data) {
              this.$Message.success('操作成功')
              this.getList()
              this.newAdmin.show_modal = false
              return
            }
            this.$Message.error(message)
          } else {
            let r = await BusinessApi.update(this.newAdmin.data)
            let message = r.message
            if (r.data) {
              this.$Message.success('操作成功')
              this.getList()
              this.newAdmin.show_modal = false
              return
            }
            this.$Message.error(message)
          }
        }
      })
    },
    // 新增商户管理员
    addAdmin() {
      let that = this
      if (this.admin.is_create) {
        this.admin.data.id = ''
        AdminApi.add(this.admin.data).then((res) => {
          let r = res
          let message = r.message
          if (r.data) {
            that.$Message.success('操作成功')
            that.getLog()
            that.admin.show_modal = false
            return
          }
          that.$Message.error(message)
        })
      } else {
        AdminApi.update(this.admin.data).then((res) => {
          let r = res
          let message = r.message
          if (r.data) {
            that.$Message.success('操作成功')
            that.getLog()
            that.admin.show_modal = false
            return
          }
          that.$Message.error(message)
        })
      }
    },
    // 新增商户商品
    addGoods() {
      let that = this
      if (this.goods.is_create) {
        this.goods.data.id = ''
        GoodsApi.add(this.goods.data).then((res) => {
          let r = res
          let message = r.message
          if (r.data) {
            that.$Message.success('操作成功')
            that.getGoods()
            that.goods.show_modal = false
            return
          }
          that.$Message.error(message)
        })
      } else {
        GoodsApi.update(this.goods.data).then((res) => {
          let r = res
          let message = r.message
          if (r.data) {
            that.$Message.success('操作成功')
            that.getGoods()
            that.goods.show_modal = false
            return
          }
          that.$Message.error(message)
        })
      }
    },
    // 入库
    put() {
      this.modal.put.loading = true
      let that = this
      BusinessApi.put(this.modal.put.formData).then(res => {
        this.modal.put.loading = false
        if (res.code == 0) {
          that.$Message.success('入库成功')
          that.modal.put.isShow = false
        } else {
          that.$Message.error(res.message)
        }
      })
    },
    changeAdmin(val) {
      this.admin.data.adminName = val.label
      this.admin.data.adminId = val.value
      this.selectAdmin.forEach((i) => {
        if (i.id == val.value) {
          this.admin.data.adminMobile = i.mobilePhone
        }
      })
    },
    changeGoods(val) {
      this.goods.data.goodsName = val.label
      this.goods.data.goodsId = val.value
      this.selectGoods.forEach((i) => {
        if (i.id == val.value) {
          this.goods.data.purchasePrice = i.price
          this.goods.data.goodsCode = i.productCore
          this.goods.data.goodsCategory = i.categoryId
          this.goods.data.thumbnail = i.picUrl
        }
      })
    },
    // 改变load状态
    changeLoading() {
      this.newAdmin.loading = false
      this.$nextTick(() => {
        this.newAdmin.loading = true
      })
    },

    // 删除
    remove(index) {
      const _id = String(index.id)
      const _name = index.name
      this.$Modal.confirm({
        title: '删除确认',
        content:
          '<p>你确认要删除管理员姓名为 <strong>' +
          _name +
          '</strong> 的记录吗？</p>',
        loading: true,
        onOk: async () => {
          let data = await $api.del(this.url + _id)
          let result = data.data
          let message = data.message
          if (result) {
            this.$Message.success('操作成功')
            this.getList()
            this.$Modal.remove()
            return
          }
          this.$Message.error(message)
          this.$Modal.remove()
        }
      })
    }
  }
}
</script>

<style>
td[class='y ivu-table-column-center'] {
  background: #02d792 !important;
  color: #fff;
}

td[class='n ivu-table-column-center'] {
  background: #f00 !important;
  color: #fff;
}

.page {
  padding: 1em 0.5em;
}
</style>
